/**
 * Copyright 2010, Lars J. Nilsson <http://www.larsan.net> 
 * 
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU Lesser General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */

package net.larsan.dconf.cli.comm;

import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.OutputStream;
import java.util.Properties;
import java.util.prefs.BackingStoreException;

import net.larsan.dconf.DNode;
import net.larsan.dconf.cli.AbstractCommand;
import net.larsan.dconf.prefs.DNodePreferences;
import net.larsan.dconf.util.PropertiesExporter;

import org.apache.commons.cli.CommandLine;
import org.apache.commons.cli.Options;

public class Export extends AbstractCommand {
	
	public Export() {
		super(new String[] { "export" }, "Export node");
	}

	@Override
	protected void doExec(CommandLine cl) {
		DNode node = findNodeFromArg(cl, 0);
		if(node == null) {
			err("No such node: " + cl.getArgs()[0]);
			return;
		}
		DNodePreferences pr = new DNodePreferences(node);
		OutputStream out = null; 
		try {
			out = getTarget(cl);
			doExport(cl, pr, out);
		} catch(BackingStoreException e) {
			err("Failed to export node: " + e.getMessage());
		} catch(IOException e) {
			err("Failed to export node: " + e.getMessage());
		} finally {
			if(cl.hasOption("f")) {
				// close if a file output
				safeClose(out);
			}
		}
	}

	@Override
	public String getArgumentSyntax() {
		return "[PATH]";
	}
	
	@Override
	protected void prepare(Options o) {
		o.addOption("r", "recursive", false, "True to export sub-tree, false nor node only");
		o.addOption("f", "file", true, "Optional file to export to");
		o.addOption("p", false, "Export to properties format instead of XML");
	}
	
	// --- PRIVATE METHODS --- //
	
	private void doExport(CommandLine cl, DNodePreferences pr, OutputStream out) throws IOException, BackingStoreException {
		if(cl.hasOption("p")) {
			exportProperties(cl, pr, out);
		} else {
			exportXml(cl, pr, out);	
		}
	}

	private void exportProperties(CommandLine cl, DNodePreferences pr, OutputStream out) throws IOException {
		PropertiesExporter exp = new PropertiesExporter();
		exp.setRecursive(cl.hasOption("r"));
		pr.getInternalNode().visit(exp);
		Properties result = exp.getTarget();
		// TODO Check second argument...
		result.store(out, "");
	}

	private void exportXml(CommandLine cl, DNodePreferences pr, OutputStream out) throws IOException, BackingStoreException {
		if(cl.hasOption("r")) {
			pr.exportSubtree(out);
		} else {
			pr.exportNode(out);
		}
	}
	
	private OutputStream getTarget(CommandLine cl) throws FileNotFoundException {
		OutputStream out = getDefaultOutput();
		if(cl.hasOption("f")) {
			out = new FileOutputStream(createFileForOutput(cl.getOptionValue("f")));
		}
		return out;
	}
}
